package com.yuejian_06.yuejian_06.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.activity.PostInfoActivity;
import com.yuejian_06.yuejian_06.bean.PostListBean;

import java.util.List;

/**
 * Created by __ZL__ on 2017/10/9.
 */

public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public PostAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v =mLayoutInflater.from(parent.getContext()).inflate(R.layout.plcardview,parent,false);
        return new PostAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) {
        final PostListBean entity =(PostListBean) mDataList.get(position);
        if (null == entity)
            return;
        PostAdapter.ViewHolder viewHolder = (PostAdapter.ViewHolder) holder;
        viewHolder.user_name.setText(entity.getUser_name());
        //String p = String.valueOf(entity.getPrice());
        viewHolder.createtime.setText(entity.getCreatetime());
        viewHolder.content.setText(entity.getContent());
        if (!"".equals(entity.getUser_pic()))
            Picasso.with(mContext).load("http://10.0.2.2/yuejian/Public/userpics/"+entity.getUser_pic()).into(viewHolder.user_pic);
        if (!"".equals(entity.getPic1()))
            Picasso.with(mContext).load("http://10.0.2.2/yuejian/Public/postpics/"+entity.getPic1()).into(viewHolder.pic1);
        if (!"".equals(entity.getPic2()))
            Picasso.with(mContext).load("http://10.0.2.2/yuejian/Public/postpics/"+entity.getPic2()).into(viewHolder.pic2);
        if (!"".equals(entity.getPic3()))
            Picasso.with(mContext).load("http://10.0.2.2/yuejian/Public/postpics/"+entity.getPic3()).into(viewHolder.pic3);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,PostInfoActivity.class);
                intent.putExtra("post_id",entity.getPost_id()+"");
                intent.putExtra("user_name",entity.getUser_name()+"");
                intent.putExtra("createtime",entity.getCreatetime()+"");
                intent.putExtra("content",entity.getContent()+"");
                intent.putExtra("user_pic",entity.getUser_pic()+"");
                intent.putExtra("pic1",entity.getPic1()+"");
                intent.putExtra("pic2",entity.getPic2()+"");
                intent.putExtra("pic3",entity.getPic3()+"");
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView user_name,createtime,content;
        private ImageView user_pic,pic1,pic2,pic3;
        public ViewHolder(View itemView) {
            super(itemView);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            createtime = (TextView) itemView.findViewById(R.id.createtime);
            content = (TextView) itemView.findViewById(R.id.content);
            user_pic = (ImageView) itemView.findViewById(R.id.user_pic);
            pic1 = (ImageView) itemView.findViewById(R.id.pic1);
            pic2 = (ImageView) itemView.findViewById(R.id.pic2);
            pic3 = (ImageView) itemView.findViewById(R.id.pic3);
        }
    }
}
